'use strict';

const Controller = require('egg').Controller;
const nodemailer = require("nodemailer")
class LoginController extends Controller {
    async passwordlogin() {
        const { ctx } = this;
        var ziduan = ctx.request.body
        console.log(ziduan, 1111111111);

        let sql1 = `SELECT * FROM user WHERE email="${ziduan.email}"`
        let re1 = await this.app.mysql.query(sql1)

        if (re1[0]) {
            //re1[0]有值说明已经注册
            // 登录
            let sql2 = `SELECT * FROM user WHERE email="${ziduan.email}" AND password="${ziduan.password}"`
            let re2 = await this.app.mysql.query(sql2)

            console.log(re2[0]);
            if (re2[0]) {
                ctx.session.email = ziduan.email
                ctx.cookies.set("email", ziduan.email, {
                    maxAge: 1000 * 60 * 60 * 24 * 365
                })
                ctx.body = {
                    info: "登录成功",
                    code: 2001
                };
            } else {
                ctx.body = {
                    info: "密码错误",
                    code: 4001
                };
            }

        } else {
            console.log(re1);

            ctx.body = {
                info: "邮箱未注册",
                code: 4002
            };
        }
    }
    // async yzmlogin() {
    //     const { ctx } = this;
    //     var ziduan = ctx.request.body
    //     // console.log(ziduan.email, 2222222222);
    //     ctx.body = {
    //         info: "",
    //         code: 4000
    //     }
    // }
    async sendEmail() {
        const {
            ctx
        } = this;
        // console.log(666666);
        var ziduan = ctx.request.body
        // console.log(ziduan);
        //将数字、小写字母及大写字母输入
        var str = "1234567890";
        //给一个空字符串
        var res = '';
        //循环4次，得到4个字符
        for (var i = 0; i < 5; i++) {
            //将得到的结果给字符串，调用随机函数，0最小数，62表示数字加字母的总数
            res += str[Math.floor(Math.random() * 10)];
        }
        console.log(res);

        var yzm = res
        let data = {
            email: ziduan.email, // 接收者的邮箱
            // 邮件模板，可自行修改
            content: `
            <head>
            <base target="_blank" />
            <style type="text/css">::-webkit-scrollbar{ display: none; }</style>
            <style id="cloudAttachStyle" type="text/css">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>
            <style id="blockquoteStyle" type="text/css">blockquote{display:none;}</style>
            <style type="text/css">
                body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
                td, input, button, select, body{font-family:Helvetica, 'Microsoft demo', verdana}
                pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}
                th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
                img{ border:0}
                header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
                blockquote{margin-right:0px}
                </style>
            </head>
            <body tabindex="0" role="listitem">
            <table width="700" border="0" align="center" cellspacing="0" style="width:700px;">
                <tbody>
                <tr>
                    <td>
                        <div style="width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;">
                            <table border="0" cellpadding="0" cellspacing="0" width="700" height="39" style="font:12px Tahoma, Arial, 宋体;">
                                <tbody><tr><td width="210"></td></tr></tbody>
                            </table>
                        </div>
                        <div style="width:680px;padding:0 10px;margin:0 auto;">
                            <div style="line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;">
                                <strong style="display:block;margin-bottom:15px;">尊敬的用户：<span style="color:#f60;font-size: 16px;"></span>您好！</strong>
                                <strong style="display:block;margin-bottom:15px;">
                                    您正在进行<span style="color: red">酷匠账号申请</span>操作，请在验证码输入框中输入：<span style="color:#f60;font-size: 24px">${yzm}</span>，以完成操作。
                                </strong>
                            </div>
                            <div style="margin-bottom:30px;">
                                <small style="display:block;margin-bottom:20px;font-size:12px;">
                                    <p style="color:#747474;">
                                        注意：此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作，请及时登录并修改密码以保证帐户安全
                                        <br>（工作人员不会向你索取此验证码，请勿泄漏！)
                                    </p>
                                </small>
                            </div>
                        </div>
                        <div style="width:700px;margin:0 auto;">
                            <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
                                <p>此为系统邮件，请勿回复<br>
                                    请保管好您的邮箱，避免账号被他人盗用
                                </p>
                                <p>Bertil Chan</p>
                            </div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>
            </body>
        `
        }
        let transporter = nodemailer.createTransport({
            host: 'smtp.163.com', //163邮箱smtp服务地址
            port: 465, // SMTP 端口
            auth: {   //发送者的账户和授权码
                user: 'lsy1626128667@163.com', //个人邮箱账户
                pass: 'FIDUUYGNFDTDHWBG', //smtp授权码，到邮箱设置下获取
            }
        });
        let mailOptions = {
            from: {
                name: '酷匠官方平台',
                address: 'lsy1626128667@163.com'
            },
            // 发送者昵称和地址
            to: data.email, // 接收者的邮箱地址
            subject: '激活验证码', // 邮件主题
            html: data.content
        };
        //发送邮件
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                return console.log(error);
            }
            console.log('邮件发送成功 ID：', info.messageId);
        });
        ctx.body = {
            info: "验证码",
            yzm,
            code: 2001

        }
    }

    async login() {
        //设置缓存
        var ziduan = this.ctx.request.body
        console.log(ziduan);
        // 判断数据库是否有该邮箱
        let sql1 = `SELECT * FROM user WHERE email="${ziduan.email}" `
        let re1 = await this.app.mysql.query(sql1)
        if (re1[0]) {
            console.log("有邮箱");
        } else {
            let sql =
                `INSERT INTO user (email) VALUES("${ziduan.email}")`
            let result = await this.app.mysql.query(sql)
        }
        this.ctx.session.email = ziduan.email
        this.ctx.cookies.set("email", ziduan.email, {
            maxAge: 1000 * 60 * 60 * 24 * 365
        })
        this.ctx.body = {
            info: "登录成功",
            code: 2002,

        }
    }
    // 清除缓存
    async nocookie() {
        let ziduan = this.ctx.request.query
        // let tel = this.ctx.session.tel
        this.ctx.session.email = null;
        // this.ctx.cookies.set("tel", tel, {
        //     maxAge: 0
        // })
        // console.log(this.ctx.cookies);

        this.ctx.body = { info: "login out success", code: 2011 }


    }
}


module.exports = LoginController;
